package app.common.utils.wdt.sdk.taobao;

import com.alibaba.fastjson.JSON;
import com.qimencloud.api.DefaultQimenCloudClient;
import com.qimencloud.api.QimenCloudClient;
import com.qimencloud.api.sceneqimen.request.TaobaoCrmOrderDetailGetRequest;
import com.qimencloud.api.sceneqimen.response.TaobaoCrmOrderDetailGetResponse;
import com.taobao.api.ApiException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class QimenCrmTest {

	public static void main(String[] args) throws ApiException
	{
		String serverUrl = "http://api.taobao.com/top/router/qmtest";

		/*
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String appKey = "appkey";// 自己的appkey
		String appSecret = "appsecret";// 自己的appsecret
		String session = "6200b284df59bc2774b613c1c3a4d3079ecace5a78f5081288922974"; // 自己的session
		String targetAppKey = "21363512";// WDT的appkey
		String wdtAppKey = "wdt_appkey";
		String wdtAppSecret = "secret:salt";
		String customerId = "customer_id";
		String wdtSalt = wdtAppSecret.split(":")[1];
		String wdtSecret = wdtAppSecret.split(":")[0];
		 */
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String appKey = "";// 自己的appkey
		String appSecret = "";// 自己的appsecret
		String session = ""; // 自己的session
		String targetAppKey = "21363512";// WDT的appkey
		String wdtAppKey = "POS_LJ_TEST";
		String wdtAppSecret = "cf0ccb4e3e72dfe2d777b02c4807e2ef:da8c99ac831c1a08a919c9d18388d60a";
//		String customerId = "customer_id";
		String wdtSalt = wdtAppSecret.split(":")[1];
		String wdtSecret = wdtAppSecret.split(":")[0];

		QimenCloudClient client = new DefaultQimenCloudClient(serverUrl, appKey, appSecret);
		TaobaoCrmOrderDetailGetRequest req = new TaobaoCrmOrderDetailGetRequest(); 
		req.setStartModified("2011-11-11 08:00:00");
		req.setEndModified("2011-12-11 08:00:00");
		req.setSdCode("ABCXXX");
		req.setOrderSn("1422XXX");
		req.setFields("order_id");
		
		// 有实际作用的参数
		req.setPageSize(20L);
		req.setPageNo(1L);

		String dateTime = sdf.format(new Date());
		Map<String, Object> extendProps = new HashMap<>();
		extendProps.put("start_time", "2020-01-05 00:00:00");
		extendProps.put("end_time", "2020-01-09 00:00:00");
		extendProps.put("wdt_appkey", wdtAppKey);
		extendProps.put("wdt_salt", wdtSalt);
		extendProps.put("datetime", dateTime);
		extendProps.put("wdt_sign", "");
		req.setExtendProps(JSON.toJSONString(extendProps));
//		req.setCustomerid(customerId);
		req.setTargetAppKey(targetAppKey);

		// !!!计算完签名之后将wdt_sign设置到extendProps节点内
		String wdtSign = Utils.getQimenOfficialWdtSign(req, wdtSecret);
		extendProps.put("wdt_sign", wdtSign);
		req.setExtendProps(JSON.toJSONString(extendProps));

		TaobaoCrmOrderDetailGetResponse response = client.execute(req, session);
		System.out.println(response.getBody());
	
	}
}
